package servlet;

import pojo.Department;
import pojo.Doctor;
import service.DoctorService;
import service.DoctorServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 陈金平
 * @Description
 * @create 2020/11/19 16:48
 */
public class AddOneDoctor extends HttpServlet {
    private DoctorService doctorService = new DoctorServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");

        Integer did = Integer.parseInt(req.getParameter("department"));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = req.getParameter("birthday");

        Doctor doctor = new Doctor();
        doctor.setName(req.getParameter("name"));
        doctor.setDid(did);
        try {
            doctor.setBirthday(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        doctor.setSex(req.getParameter("sex"));
        doctor.setDid(did);

        if (doctorService.addOneDoctor(doctor) > 0) {
            req.setAttribute("message", "新增成功");
        }
        else {
            req.setAttribute("message", "新增失败");
        }

        req.getRequestDispatcher("get_message.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
